package com.yanqu.road.server.cmd.activity.ppl;

import com.yanqu.road.entity.activity.ppl.PplUserData;
import com.yanqu.road.server.pb.PplPb;
import com.yanqu.road.pb.activity.PplProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ppl.PplModule;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_ACTIVITY_PPL_GAME_DATA, desc = "泡泡龙获取小游戏数据")
public class GetGameDataCmd extends AbstractCommand {

    private int multiType = 1;

    private PplProto.PplGameDataMsg.Builder gameDataMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        if (packet.getBytes() == null) {
            return true;
        }
        PplProto.GetGameDataReqMsg reqMsg = PplProto.GetGameDataReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasMultiType()) {
            multiType = reqMsg.getMultiType();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (multiType != 1 && multiType != 2 && multiType != 3 && multiType != 4) {
            ret = GameErrorCode.E_ACTIVITY_PPL_MULTI_TYPE_ERROR;
            return;
        }
        if (!PplMgr.isActivityInTime()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_NO_IN_TIME;
            return;
        }
        PplModule pplModule = player.getModule(PplModule.class);
        pplModule.checkEnergyRecovery();
        if (!pplModule.isBeautyChosen()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_BEAUTY_NO_CHOSEN;
            return;
        }

        PplUserData userData = pplModule.getUserData();
        gameDataMsg = PplPb.parsePplGameDataMsg(userData, multiType);
    }

    @Override
    public void sendData(GamePlayer player) {
        PplProto.GetGameDataRespMsg.Builder builder = PplProto.GetGameDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setGameData(gameDataMsg);
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_PPL_GAME_DATA, builder);
    }
}
